package com.cz.nongyetongv5.adapter;

import android.content.Context;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.custom.badgeview.BadgeView;
import com.cz.nongyetongv5.custom.wheelview.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TouRuPinIconGrideAdapter7 extends BaseAdapter {

	private List<Map<String, Object>> iconGridData = null;

	private LayoutInflater layoutInflater = null;

	private OnClickListener onClickListener = null;

	private Context context = null;

	private List<View> views = new ArrayList<View>();

	public TouRuPinIconGrideAdapter7(Context context,
			List<Map<String, Object>> iconGridData,
			LayoutInflater layoutInflater, OnClickListener onClickListener) {
		this.iconGridData = iconGridData;
		this.layoutInflater = layoutInflater;
		this.onClickListener = onClickListener;
		this.context = context;
	}

	@Override
	public int getCount() {
		return iconGridData.size();
	}

	@Override
	public Object getItem(int position) {
		return iconGridData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewGroup iconLayout = null;

		int ibIconItem = (Integer) iconGridData.get(position).get("ibIconItem");

		String iconText = (String) iconGridData.get(position).get("tvIconItem");

		
		
		if (convertView != null) {
			iconLayout = (ViewGroup) convertView;
		} else {
			iconLayout = (ViewGroup) layoutInflater.inflate(
					R.layout.gridview_item, null);
		}
		ViewGroup rlIconLayout = (ViewGroup) iconLayout
				.findViewById(R.id.rlIconLayout);
		ImageView imageButton = (ImageView) iconLayout
				.findViewById(R.id.ibIconItem);

		imageButton.setBackgroundResource(ibIconItem);

		imageButton.setTag(iconText);

		imageButton.setOnClickListener(onClickListener);

		TextView textView = (TextView) iconLayout.findViewById(R.id.tvIconItem);

		 textView.setText(iconText);
	
		
		 if(iconGridData.get(position).get("count") != null) {
				int count = 0;
				try{
				  count=(Integer) iconGridData.get(position).get("count");
				}catch(Exception e){
					e.printStackTrace();
					count = 0;
				}
				 System.out.println(count+"");
				BadgeView badge = (BadgeView) rlIconLayout.getTag();

				if (badge == null) {

					badge = new BadgeView(context, rlIconLayout);
					badge.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
					badge.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);
					badge.setGravity(Gravity.CENTER);

					int topVal = Utils.dip2px(context, 12);
					int leftVal = Utils.dip2px(context, 3);

					badge.setBadgeMargin(badge.getLeft() + badge.getWidth()
							+ leftVal, badge.getTop() + topVal);

					rlIconLayout.setTag(badge);
				}

				String txt = count + "";

				if (txt.length() > 2) {

					int paddTop = Utils.dip2px(context, 2);
					int paddBottom = Utils.dip2px(context, 2);

					badge.setPadding(badge.getPaddingLeft(), paddTop,
							badge.getPaddingRight(), paddBottom);

					badge.setText("99+");
				} else {

					badge.setText(count + "");

				}

				if (count > 0) {
					badge.show();
				} else {
					badge.hide();
				}

			}
		return iconLayout;
	}

	@Override
	public void notifyDataSetChanged() {
		views.clear();

		super.notifyDataSetChanged();
	}

	public View getView(int position) {

		return views.get(position);
	}

}
